'\" te
.\" Copyright (C) 2009, Sun Microsystems, Inc. All Rights Reserved.
.\" Copyright 1989 AT&T
.\" Copyright (c) 2013, OmniTI Computer Consulting, Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.\" Copyright 2022 Garrett D'Amore
.TH SOCKET 3SOCKET "July 6, 2022"
.SH NAME
socket \- create an endpoint for communication
.SH SYNOPSIS
.nf
\fBcc\fR [ \fIflag\fR ... ] \fIfile\fR ... \fB-lsocket\fR \fB -lnsl \fR [ \fIlibrary\fR ... ]
#include <sys/types.h>
#include <sys/socket.h>

\fBint\fR \fBsocket\fR(\fBint\fR \fIdomain\fR, \fBint\fR \fItype\fR, \fBint\fR \fIprotocol\fR);
.fi

.SH DESCRIPTION
The \fBsocket()\fR function creates an endpoint for communication and returns a
descriptor.
.LP
The \fIdomain\fR argument specifies the protocol family within which
communication takes place. The protocol family is generally the same as the
address family for the addresses supplied in later operations on the socket.
These families are defined in \fB<sys/socket.h>\fR.
.sp
.LP
The currently supported protocol families are:
.sp
.ne 2
.na
\fB\fBPF_UNIX\fR\fR
.ad
.RS 12n
\fBUNIX\fR system internal protocols
.RE

.sp
.ne 2
.na
\fB\fBPF_INET\fR\fR
.ad
.RS 12n
Internet Protocol Version 4 (IPv4)
.RE

.sp
.ne 2
.na
\fB\fBPF_INET6\fR\fR
.ad
.RS 12n
Internet Protocol Version 6 (IPv6)
.RE

.sp
.LP
The socket has the indicated \fItype\fR, which specifies the communication
semantics. Currently defined types are:
.sp
.in +2
.nf
SOCK_STREAM
SOCK_DGRAM
SOCK_RAW
SOCK_SEQPACKET
SOCK_RDM
.fi
.in -2

.sp
.LP
The \fItype\fR may be augmented by a bitwise-inclusive-OR of flags from the
following list, defined in <sys/socket.h>.

.sp
.ne 2
.na
\fB\fBSOCK_CLOEXEC\fR\fR
.ad
.RS 12n
Creates the socket with the \fBFD_CLOEXEC\fR flag set, causing the underlying
file descriptor to be closed prior to any future calls to \fBexec\fR(2). This
is similar in purpose to the \fBO_CLOEXEC\fR flag to \fBopen\fR(2).
.RE

.sp
.ne 2
.na
\fB\fBSOCK_NDELAY\fR\fR
.ad
.RS 12n
Creates the socket with the \fBO_NDELAY\fR flag set, causing the socket to
provide nonblocking semantics as described for \fBO_NDELAY\fR in \fBopen\fR(2).
\fBSOCK_NONBLOCK\fR should normally be used in preference to \fBSOCK_NDELAY\fR,
and takes precedence if both are set.  See \fBopen\fR(2) for further details.
.RE

.sp
.ne 2
.na
\fB\fBSOCK_NONBLOCK\fR\fR
.ad
.RS 12n
Creates the socket with the \fBO_NONBLOCK\fR flag set, causing the socket to
provide nonblocking semantics as described for \fBO_NONBLOCK\fR in \fBopen\fR(2).
.RE

.sp
.LP
There must be an entry in the \fBnetconfig\fR(5) file for at least each
protocol family and type required. If  a non-zero protocol has been specified
but no exact match for the protocol family, type, and protocol is found, then
the first entry containing the specified family and type with a \fIprotocol\fR
value of zero will be used.
.sp
.LP
A \fBSOCK_STREAM\fR type provides sequenced, reliable, two-way connection-based
byte streams. An out-of-band data transmission mechanism may be supported. A
\fBSOCK_DGRAM\fR socket supports datagrams (connectionless, unreliable messages
of a fixed (typically small) maximum length). A \fBSOCK_SEQPACKET\fR socket may
provide a sequenced, reliable, two-way connection-based data transmission path
for datagrams of fixed maximum length; a consumer may be required to read an
entire packet with each read system call. This facility is protocol specific,
and presently not implemented for any protocol family. \fBSOCK_RAW\fR sockets
provide access to internal network interfaces. The types \fBSOCK_RAW\fR, which
is available only to a user with the \fBnet_rawaccess\fR privilege, and
\fBSOCK_RDM\fR, for which no implementation currently exists, are not described
here.
.sp
.LP
The \fIprotocol\fR parameter is a protocol-family-specific value which
specifies a particular protocol to be used with the socket.  Normally this
value is zero, as commonly only a single protocol exists to support a
particular socket type within a given protocol family. However, multiple
protocols may exist, in which case a particular protocol may be specified in
this manner.
.sp
.LP
Sockets of type \fBSOCK_STREAM\fR are full-duplex byte streams, similar to
pipes. A stream socket must be in a \fIconnected\fR state before any data may
be sent or received on it. A connection to another socket is created with a
\fBconnect\fR(3SOCKET) call. Once connected, data may be transferred using
\fBread\fR(2) and \fBwrite\fR(2) calls or some variant of the
\fBsend\fR(3SOCKET) and \fBrecv\fR(3SOCKET) calls. When a session has been
completed, a \fBclose\fR(2) may be performed. Out-of-band data may also be
transmitted as described on the \fBsend\fR(3SOCKET) manual page and received as
described on the \fBrecv\fR(3SOCKET) manual page.
.sp
.LP
The communications protocols used to implement a \fBSOCK_STREAM\fR insure that
data is not lost or duplicated.  If a piece of data for which the peer protocol
has buffer space cannot be successfully transmitted within a reasonable length
of time, then the connection is considered broken and calls will indicate an
error with \(mi1 returns and with \fBETIMEDOUT\fR as the specific code in the
global variable \fBerrno\fR. The protocols optionally keep sockets "warm" by
forcing transmissions roughly every minute in the absence of other activity. An
error is then indicated if no response can be elicited on an otherwise idle
connection for a extended period (for instance 5 minutes). A \fBSIGPIPE\fR
signal is raised if a thread sends on a broken stream; this causes naive
processes, which do not handle the signal, to exit.
.sp
.LP
\fBSOCK_SEQPACKET\fR sockets employ the same system calls as \fBSOCK_STREAM\fR
sockets. The only difference is that  \fBread\fR(2) calls will return only the
amount of data requested, and any remaining in the arriving packet will be
discarded.
.sp
.LP
\fBSOCK_DGRAM\fR and \fBSOCK_RAW\fR sockets allow datagrams to be sent to
correspondents named in \fBsendto\fR(3SOCKET) calls. Datagrams are generally
received with \fBrecvfrom\fR(3SOCKET), which returns the next datagram with its
return address.
.sp
.LP
An \fBfcntl\fR(2) call can be used to specify a process group to receive a
\fBSIGURG\fR signal when the out-of-band data arrives. It can also enable
non-blocking I/O.
.sp
.LP
The operation of sockets is controlled by socket level \fIoptions\fR. These
options are defined in the file <\fBsys/socket.h\fR>. \fBsetsockopt\fR(3SOCKET)
and \fBgetsockopt\fR(3SOCKET) are used to set and get options, respectively.
.SH RETURN VALUES
Upon successful completion, a descriptor referencing the socket is returned.
Otherwise, -1 is returned and \fBerrno\fR is set to indicate the error.
.SH ERRORS
The \fBsocket()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 19n
Permission to create a socket of the specified type or protocol is denied.
.RE

.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 19n
There were insufficient resources available to complete the operation.
.RE

.sp
.ne 2
.na
\fB\fBEAFNOSUPPORT\fR\fR
.ad
.RS 19n
The specified address family is not supported by the protocol family.
.RE

.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 19n
The per-process descriptor table is full.
.RE

.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 19n
Insufficient user memory is available.
.RE

.sp
.ne 2
.na
\fB\fBENOSR\fR\fR
.ad
.RS 19n
There were insufficient STREAMS resources available to complete the operation.
.RE

.sp
.ne 2
.na
\fB\fBEPFNOSUPPORT\fR\fR
.ad
.RS 19n
The specified protocol family is not supported.
.RE

.sp
.ne 2
.na
\fB\fBEPROTONOSUPPORT\fR\fR
.ad
.RS 19n
The protocol type is not supported by the address family.
.RE

.sp
.ne 2
.na
\fB\fBEPROTOTYPE\fR\fR
.ad
.RS 19n
The socket type is not supported by the protocol.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 19n
One or more of the specified flags is not supported.
.RE

.SH ATTRIBUTES
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
MT-Level	Safe
.TE

.SH SEE ALSO
.BR close (2),
.BR exec (2),
.BR fcntl (2),
.BR ioctl (2),
.BR open (2),
.BR read (2),
.BR write (2),
.BR in.h (3HEAD),
.BR socket.h (3HEAD),
.BR accept (3SOCKET),
.BR bind (3SOCKET),
.BR connect (3SOCKET),
.BR getsockname (3SOCKET),
.BR getsockopt (3SOCKET),
.BR listen (3SOCKET),
.BR recv (3SOCKET),
.BR send (3SOCKET),
.BR setsockopt (3SOCKET),
.BR shutdown (3SOCKET),
.BR socketpair (3SOCKET),
.BR attributes (7)
.SH NOTES
Historically, \fBAF_\fR* was commonly used in places where \fBPF_\fR* was
meant. New code should be careful to use \fBPF_\fR* as necessary.
